package org.lex.ftp.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class MenuButton {
	public static JToggleButton create(final JPopupMenu popup) {
		final JToggleButton button = new JToggleButton();
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				popup.show(button, 0, button.getHeight());
			}
		});
		popup.addPopupMenuListener(new PopupMenuListener() {
			@Override
			public void popupMenuCanceled(PopupMenuEvent e) {
				button.setSelected(false);
			}

			@Override
			public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
				button.setSelected(false);
			}

			@Override
			public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
			}
		});
		return button;
	}
}
